package com.fare.taluo.action.system;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.application.system.auth.UserInfoLogApplication;
import com.fare.taluo.base.struts2.json.BaseJsonStrutsAction;
import com.fare.taluo.base.struts2.json.JsonReturnBean;
import com.fare.taluo.util.DateJsonValueProcessor;
import com.fare.taluo.vo.auth.RegisterVo;
import com.fare.taluo.vo.auth.UserInfoLoginVO;
@Controller("userInfoLogAction")
@Scope("prototype")
@Namespace("/system/log")
public class UserInfoLogAction extends BaseJsonStrutsAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	private UserInfoLogApplication userInfoLogApplication;
	private RegisterVo registerVo;
	private List<UserInfoLoginVO> listVo;
	private int count;
	private String data;
	private String loginTimeStr;
	private String logoutTimeStr;
	
	/*
	 * 页面跳转
	*/
	@Action(value = "toUserInfoLogJSP", results = {@Result(name = "success", type="dispatcher", location="/pages/auth/UserLogInfo-list.jsp") })
	public String toUserInfoLogJSP(){
		count=userInfoLogApplication.countUserOnline();
		loginTimeStr=request.getParameter("loginTimeStr");
		logoutTimeStr=request.getParameter("logoutTimeStr");
		listVo=userInfoLogApplication.findAll(loginTimeStr,logoutTimeStr);
		JsonConfig jsc = new JsonConfig();
		jsc.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		this.data=JSONArray.fromObject(this.listVo,jsc).toString();
		return SUCCESS;
	}
	
	//插入信息
	public String inputLog(UserInfoLoginVO vo){
		if(vo !=null){
			userInfoLogApplication.saveOrUpdate(vo);
		}
		return "dispatcher";
	}
	
	//验证用户是否已经登录
	@Action(value = "checkUserLogin", results = {@Result(name = "success", type="json", params={"root","returnBean"}) })
	public String checkUserLogin(){
		returnBean = new JsonReturnBean();
		UserInfoLoginVO vo =new UserInfoLoginVO();
		String userNmae=null;
		userNmae=request.getParameter("userName"); 
		try{
			if(userNmae!=null && !userNmae.equals("")){
				Object obj =request.getSession().getAttribute(userNmae);
//				vo=userInfoLogApplication.checkUserLogin(userNmae);
//				if(vo!=null && vo.getLogoutTimeStr()!=null && !vo.getLogoutTimeStr().equals("")&&vo.getOnlineTimeStr() !=null && !vo.getOnlineTimeStr().equals("")){
				if(obj==null){
					returnBean.setHasOk(true);
				}else{
					returnBean.setHasOk(false);
					returnBean.setMessage(userNmae+"用户已登录!");
				}
			}else{
				returnBean.setHasOk(false);
				returnBean.setMessage("用户名不能为空!");
			}
		}catch (Exception e) {
			e.printStackTrace();
			returnBean.setHasOk(false);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}

	public RegisterVo getRegisterVo() {
		return registerVo;
	}

	public void setRegisterVo(RegisterVo registerVo) {
		this.registerVo = registerVo;
	}

	public List<UserInfoLoginVO> getListVo() {
		return listVo;
	}

	public void setListVo(List<UserInfoLoginVO> listVo) {
		this.listVo = listVo;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}
	
	
}
